package com.freak.freakmusic.view.load.indicators


import android.animation.ValueAnimator
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import com.freak.freakmusic.view.load.Indicator
import java.util.*

/**
 * Created by Jack on 2015/10/16.
 */
class BallClipRotateIndicator : Indicator() {

    internal var scaleFloat = 1f
    internal var degrees: Float = 0.toFloat()

    override fun draw(canvas: Canvas, paint: Paint) {
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 3f

        val circleSpacing = 12f
        val x = width / 2
        val y = height / 2
        canvas.translate(x.toFloat(), y.toFloat())
        canvas.scale(scaleFloat, scaleFloat)
        canvas.rotate(degrees)
        val rectF = RectF(
            -x + circleSpacing,
            -y + circleSpacing,
            0 + x - circleSpacing,
            0 + y - circleSpacing
        )
        canvas.drawArc(rectF, -45f, 270f, false, paint)
    }

    override fun onCreateAnimators(): ArrayList<ValueAnimator> {
        val animators = ArrayList<ValueAnimator>()
        val scaleAnim = ValueAnimator.ofFloat(
            1F, 0.6f, 0.5f, 1F
        )
        scaleAnim.duration = 750
        scaleAnim.repeatCount = -1
        addUpdateListener(scaleAnim, ValueAnimator.AnimatorUpdateListener { animation ->
            scaleFloat = animation.animatedValue as Float
            postInvalidate()
        })
        val rotateAnim = ValueAnimator.ofFloat(0F, 180F, 360F)
        rotateAnim.duration = 750
        rotateAnim.repeatCount = -1
        addUpdateListener(rotateAnim, ValueAnimator.AnimatorUpdateListener { animation ->
            degrees = animation.animatedValue as Float
            postInvalidate()
        })
        animators.add(scaleAnim)
        animators.add(rotateAnim)
        return animators
    }

}
